package com.zd.server.sftpserver.config;

import com.zd.server.sftpserver.sftp.SftpServer;
import com.zd.server.sftpserver.sftp.SftpUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Component
public class SftpServerFactory {
    @Value("${sftp.server.generatorhostkeypath:hostkey.ser}")
    private String generatorHostKeyPath;

    private ConcurrentHashMap<String, SftpServer> sftpServerMap = new ConcurrentHashMap<>();

    private SftpServer getSftpServer(String host,Integer port) {
        return sftpServerMap.get(host+":"+port);
    }
    private void checkParam(String host,Integer port) {
        if (StringUtils.hasText(host) && port != null) {
            return;
        }else {
            throw new RuntimeException("参数不能为空");
        }
    }
    private void checkParam(String host,Integer port,String userName,String password,String home) {
        if (StringUtils.hasText(host) && port != null && StringUtils.hasText(userName) && StringUtils.hasText(password) && StringUtils.hasText(home)) {
            return;
        }else {
            throw new RuntimeException("参数不能为空");
        }
    }
    public void addSftpServer(String host,Integer port) {
        checkParam(host,port);
        if (!sftpServerMap.containsKey(host + ":" + port)) {
            sftpServerMap.put(host + ":" + port, new SftpServer(host, port,generatorHostKeyPath));
            log.info("添加服务器成功{}：{}", host, port);
        } else {
            log.info("服务器已存在{}：{}", host, port);
        }
    }
    public void removeSftpServer(String host,Integer port) {
        checkParam(host,port);
        try {
            getSftpServer(host,port).stop();
        } catch (IOException e) {
            log.error("停止服务异常{}：{}",host,port,e);
        }finally {
            sftpServerMap.remove(host+":"+port);
        }
    }

    /**
     * 添加用户
     */
    public void addUser(String host,Integer port,String username,String password,String home) {
        checkParam(host,port,username,password,home);
        SftpUser sftpUser = new SftpUser(username, password, home);
        SftpServer sftpServer = getSftpServer(host, port);
        if (sftpServer != null) {
            sftpServer.addUser(sftpUser);
        }else {
            throw new RuntimeException("服务器不存在");
        }
    }
    /**
     * 删除用户
     */
    public void removeUser(String host,Integer port,String username) {
        checkParam(host,port);
        SftpServer sftpServer = getSftpServer(host, port);
        if (sftpServer != null) {
            sftpServer.removeUser(username);
        }
    }

    /**启动服务器
     * @param host
     */
    public void startSftpServer(String host,Integer port) {
        checkParam(host,port);
        try {
            getSftpServer(host,port).start();
        } catch (IOException e) {
            log.error("启动服务异常{}：{}",host,port,e);
        }
    }
    /**停止服务器
     * @param host
     */
    public void stopSftpServer(String host,Integer port) {
        checkParam(host,port);
        try {
            SftpServer sftpServer = getSftpServer(host, port);
            sftpServer.stop();
            removeSftpServer(host,port);
        } catch (IOException e) {
            log.error("停止服务异常{}：{}",host,port,e);
        }
    }
    /**
     * 停止所有服务器
     */
    public void stopAllSftpServer() {
        sftpServerMap.forEach((key,value)->{
            try {
                value.stop();
            } catch (IOException e) {
                log.error("停止服务异常{}：{}",key,e);
            }
        });
        sftpServerMap.clear();
    }

    /**
     * 启动所有服务器
     */
    public void startAllSftpServer() {
        sftpServerMap.forEach((key,value)->{
            try {
                value.start();
            } catch (IOException e) {
                log.error("启动服务异常{}：{}",key,e);
            }
        });
    }


    public Map mapSftpServer() {
        return sftpServerMap;
    }
}
